const state = {
    categories: [],
    currentCategory: null
  }
  
  const mutations = {
    SET_CATEGORIES: (state, categories) => {
      state.categories = categories
    },
    SET_CURRENT_CATEGORY: (state, category) => {
      state.currentCategory = category
    }
  }
  
  const actions = {
    // 获取所有栏目
    getCategories({ commit }) {
      return new Promise((resolve, reject) => {
        getCategoryList().then(response => {
          commit('SET_CATEGORIES', response.data)
          resolve(response.data)
        }).catch(error => {
          reject(error)
        })
      })
    },
    
    // 获取当前栏目
    getCurrentCategory({ commit }, id) {
      return new Promise((resolve, reject) => {
        getCategoryById(id).then(response => {
          commit('SET_CURRENT_CATEGORY', response.data)
          resolve(response.data)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
  
  const getters = {
    topCategories: state => {
      return state.categories.filter(category => category.parentId === 0)
    },
    visibleCategories: state => {
      return state.categories.filter(category => category.visible === '0')
    }
  }
  
  export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
  }